/**
 * 违章审核记录 列表页
 *
 * 作者 Wanghw
 * 日期 2024-08-14 10:38:38
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-peccancyAppealRecord'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#peccancyAppealRecord-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate();
    initTable();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            range: ['#start-date', '#end-date'],
            format: 'yyyy-MM-dd',
            btns:['now','confirm']
        })
        $('#end-date').val(commonJS.getCurrentDateStr(new Date()));
        $('#start-date').val(commonJS.getPastDateStr(new Date(),7));
        form.render();

    }
    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'peccancyAppealRecordTable',
            url: ctx + 'peccancyAppealRecord/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers',title:'序号'},
                {field: 'informationId', title: 'id', minWidth: 120,hide:true},
                {field: 'tenantId', title: '公司名称', minWidth: 100,hide:!hasHeadCompanyRole,templet:function (d) {
                        var tenant = tenantList.filter(item=>item.tenantId == d.tenantId);
                        return tenant[0].tenantName;
                    }},
                {field: 'peccancyNo', title: '单号', width: 175,align:'center', event: 'detail',templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.peccancyNo+'</p>'
                    }},
                {field: 'projectName', title: '项目名称', minWidth: 120},
                {
                    field: 'peccancyObject', title: '违章对象', width: 100, align: 'center', templet: function (d) {
                        if (d.peccancyObject == '1') {
                            return '<span class="layui-badge febs-tag-green">本工</span>';
                        } else if (d.peccancyObject == '2') {
                            return '<span class="layui-badge febs-tag-magenta">外包</span>';
                        } else if (d.peccancyObject == '3') {
                            return '<span class="layui-badge febs-tag-orange">外协</span>';
                        } else if (d.peccancyObject == '4') {
                            return '<span class="layui-badge febs-tag-cyan">承包队</span>';
                        } else if (d.peccancyObject == '5') {
                            return '<span class="layui-badge febs-tag-purple">部门</span>';
                        }
                    }
                },
                {
                    field: 'peccancyRealName', title: '违章对象名称', minWidth: 230, templet: function (d) {
                        var html = '';
                        if (d.peccancyObject == '4') {
                            if (d.peccancyUserContractorTeamName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-team febs-blue"></i>'
                                html+=d.peccancyUserContractorTeamName
                            }
                        }else if (d.peccancyObject == '5') {
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                        } else {
                            if (d.peccancyRealName) {
                                html = '<i class="layui-icon layui-icon-user febs-blue"></i>'
                                html+=d.peccancyRealName
                            }
                            if (d.peccancyUserDept) {
                                html += '&nbsp;<i class="layui-icon layui-icon-idcard febs-blue"></i>'
                                html+=d.peccancyUserDept
                            }
                            if (d.peccancyUserPostName) {
                                html += '&nbsp;<i class="layui-icon layui-icon-creditcard febs-blue"></i>'
                                html+=d.peccancyUserPostName
                            }

                        }
                        return html
                    }
                },
                {field: 'jobOperation', title: '违章作业类别', width: 120 ,align:'center',templet:function (d){
                        if(d.jobOperation){
                            return febs.getDictLabel(jobOperationList,d.jobOperation)
                        }
                    }},
                {field: 'peccancyType', title: '违章类型', width: 120 ,align:'center',templet:function (d){
                        if(d.peccancyType){
                            return febs.getDictLabel(peccancyTypeList,d.peccancyType)
                        }
                    }},
                {field: 'peccancyRootCause', title: '违章原因', width: 140,align:'center',templet:function (d){
                        if(d.peccancyRootCause){
                            return febs.getDictLabel(peccancyRootCauseList,d.peccancyRootCause)
                        }
                    }},
                {field: 'invoiceRealName', title: '开单用户姓名', width: 120,align:'center'},
                {field: 'invoiceDate', title: '开单时间', width: 160,align:'center'},
                {field: 'appealReason', title: '申诉原因', minWidth: 160,align:'center'},
                {field: 'status', title: '状态', width: 100 ,align:'center', templet: function (d) {
                        if(d.status=='1'){
                            return  '<span class="layui-badge febs-tag-green">待提交</span>';
                        }else if(d.status=='2'){
                            return  '<span class="layui-badge febs-tag-magenta">待审核</span>';
                        }else if(d.status=='3'){
                            return  '<span class="layui-badge febs-tag-orange">审核退回</span>';
                        }else if(d.status=='4'){
                            return  '<span class="layui-badge febs-tag-cyan">待签发</span>';
                        }else if(d.status=='5'){
                            return  '<span class="layui-badge febs-tag-purple">签发退回</span>';
                        }else if(d.status=='6'){
                            return  '<span class="layui-badge febs-tag-lime">已签发</span>';
                        }else if(d.status=='10'){
                            return  '<span class="layui-badge febs-tag-red">已作废</span>';
                        }
                    }},
                {title: '操作', toolbar: '#peccancyAppealRecord-tool',align:'center', width: 180,fixed:'right'}
            ]]
        });
    }

    table.on('sort(peccancyAppealRecordTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });


    table.on('tool(peccancyAppealRecordTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'appeal') {
            febs.modal.open('申诉违章记录', 'peccancyAppealRecord/update/' + data.informationId, {
                area: $(window).width() <= 750 ? ['80%','95%']: ['70%', '90%'],
                btn: ['申诉', '取消'],
                yes: function (index, layero) {
                    $('#peccancyAppealRecord-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }else if (layEvent === 'detail') {
            febs.modal.open('违章信息详情', 'peccancyInformation/detail/' + data.informationId, {
                area: $(window).width() <= 750 ? '90%' : ['70%', '90%'],
                shadeClose: true,
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.close(index)
                },
            });
        }else if (layEvent === 'appealUpdate') {
            febs.modal.open('申诉违章记录', 'peccancyAppealRecord/update/' + data.informationId, {
                area: $(window).width() <= 750 ? ['80%','95%']:['70%', '90%'],
                btn: ['修改', '取消'],
                yes: function (index, layero) {
                    $('#peccancyAppealRecord-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }
    });



    function getQueryParams() {
        return {
            projectName: $searchForm.find('input[name="projectName"]').val(),
            peccancyRealName: $searchForm.find('input[name="peccancyRealName"]').val(),
            tenantId: $searchForm.find('select[name="tenantId"]').find("option:selected").val(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            peccancyType: $searchForm.find("select[name='peccancyType']").find("option:selected").val(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            invalidate_ie_cache: new Date()
        };
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});